package sharewithus.analyzer.config;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.InvalidPropertiesFormatException;
import java.util.List;
import java.util.Properties;

/**
 * 扩展词典配置类
 * @author terry
 *
 */
public class Configuration {
	
	/**
	 * 配置文件路径
	 */
	private static final String PATH_CONFIG = "/IKAnalyzer.cfg.xml";
	
	//配置属性——扩展字典
	private static final String EXT_DIC = "ext_dic";
	
	//配置属性——扩展二元词典
	private static final String EXT_BIGRAM = "ext_bigram";
	
	private static final Configuration CFG = new Configuration();
	
	private Properties props;
	
	/*
	 * 初始化配置文件
	 */
	private Configuration(){
		
		props = new Properties();
		
		InputStream input = Configuration.class.getResourceAsStream(PATH_CONFIG);
		if(input != null){
			try {
				props.loadFromXML(input);
			} catch (InvalidPropertiesFormatException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 获取扩展二元词典配置
	 * @return List<String> 相对类加载器的词性、路径、词频
	 */
	public static List<String> getExtBigramDictionarys(){
		List<String> extBigramFiles = new ArrayList<String>(2);
		String extBigramCfg = CFG.props.getProperty(EXT_BIGRAM);
		if(extBigramCfg != null){
			//使用;分割多个扩展字典配置
			String[] filePaths = extBigramCfg.split(";");
			if(filePaths != null){
				for(String filePath : filePaths){
					if(filePath != null && !"".equals(filePath.trim())){
						extBigramFiles.add(filePath.trim());
						//System.out.println(filePath.trim());
					}
				}
			}
		}		
		return extBigramFiles;		
	}
	
	/**
	 * 获取扩展词典配置
	 * @return List<String> 词典的词性、路径、词频,三者用:隔开
	 */
	public static List<String> getExtDictionarys(){
		List<String> extDictFiles = new ArrayList<String>(2);
		String extDictCfg = CFG.props.getProperty(EXT_DIC);
		if(extDictCfg != null){
			//使用;分割多个扩展字典配置
			String[] filePaths = extDictCfg.split(";");
			if(filePaths != null){
				for(String info : filePaths){
					if(info != null && !"".equals(info.trim())){
						extDictFiles.add(info.trim());
						//System.out.println(filePath.trim());
					}
				}
			}
		}		
		return extDictFiles;		
	}
}
